package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class ComboDataController {
	private static final Logger logger = LoggerFactory
			.getLogger(ComboDataController.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@RequestMapping(value = "/getComboData")
	public void getComboData(HttpSession session,
			HttpServletRequest request, HttpServletResponse response,
			PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		HashMap<String,Object> param = new HashMap<String,Object>();
		try {
			String lang = session.getAttribute(WebConstants.CURRENT_LANG).toString();
			String type = request.getParameter("type");
			
			String sql = "select TEXT as text,VALUE as value from TAX_C_COM_MDATA WHERE LANG = '" + lang + "' and TYPE = '" + type + "' order by value";
			param.put("type", type);
			param.put("lang", lang);
			@SuppressWarnings({ "unchecked", "rawtypes" })
			ArrayList<HashMap<String,Object>> result = (ArrayList<HashMap<String, Object>>) jdbcTemplate.query(sql,new RowMapper() {
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					HashMap<String,Object> post = new HashMap<String,Object>();
					post.put("text", rs.getObject(1));
					post.put("value", rs.getObject(2));
					return post;
				}
			});
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
	
	@RequestMapping(value = "/getComboDataNoOrder")
	public void getComboDataNoOrder(HttpSession session,
			HttpServletRequest request, HttpServletResponse response,
			PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		HashMap<String,Object> param = new HashMap<String,Object>();
		try {
			String lang = session.getAttribute(WebConstants.CURRENT_LANG).toString();
			String type = request.getParameter("type");
			
			String sql = "select TEXT as text, VALUE as value from TAX_C_COM_MDATA WHERE LANG = '" + lang + "' and TYPE = '" + type +"'";
			param.put("type", type);
			param.put("lang", lang);
			@SuppressWarnings({ "unchecked", "rawtypes" })
			ArrayList<HashMap<String,Object>> result = (ArrayList<HashMap<String, Object>>) jdbcTemplate.query(sql,new RowMapper() {
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					HashMap<String,Object> post = new HashMap<String,Object>();
					post.put("text", rs.getObject(1));
					post.put("value", rs.getObject(2));
					return post;
				}
			});
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
}